package com.henz.web.controller;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.henz.web.domain.HenzUser;
import com.henz.web.service.HenzUserService;
import com.henz.web.service.impl.HenzUserServiceImpl;

@Controller
@RequestMapping("/user")
public class UserController {
	
	Log logger = LogFactory.getLog(this.getClass());
	
	//private HenzUserService henzUserService;
	
	@RequestMapping("/{name}.do")
	public String getAddCustomerPage(@PathVariable String name) {
		return "user/" + name;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/save.json")
	public @ResponseBody List<Map<String, Object>> saveUser(
		@RequestBody Map<String, Object> paramMap
	) {
		HenzUserService henzUserService = null;
		henzUserService = new HenzUserServiceImpl();
		List<Map<String, Object>> userList = null;
		Iterator<String> keyIt = null;
		String key = null;
		
		keyIt = paramMap.keySet().iterator();
		
		while(keyIt.hasNext()){
			key = keyIt.next();
			logger.info("key[" + key + "][" + "]");
			
		}
		logger.info("userList=================");
		userList = (List<Map<String, Object>>)paramMap.get("userList");
		logger.info(userList);
		if(userList != null){
			for( Map<String, Object> user: userList ){
				logger.info("user=================");
				logger.info(user);
				logger.info("user=================");
			}
		}
		logger.info("userList=================");
		
		logger.info("henzUserService=================");
		logger.info(henzUserService);
		logger.info("henzUserService=================");
		
		return henzUserService.saveUser(userList);
		
	}
	
	@RequestMapping("/listold.json")
	public @ResponseBody Collection<HenzUser> findAllUsers(
		@RequestBody Map<String, Object> paramMap
	) {
		//return henzService.findAllUsers();
		//return henzService.findAllUsers(paramMap);
		return null;
	}
	
	@RequestMapping("/list.json")
	public @ResponseBody List<Map<String, Object>> findAllUsersOld(
		@RequestBody Map<String, Object> paramMap
	) {
		HenzUserService userService = null;
		userService = new HenzUserServiceImpl();
		return userService.findAllUsers();
	}
	
	
}